<?php


namespace app\api\exception;

use Exception;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;

class Http extends Handle
{
    public function render(Exception $e)
    {
        // 参数验证错误
        if ($e instanceof ValidateException) {
            json([
                'code' => 422,
                'msg' => $e->getMessage(),
                'data' => $e->getHeaders()
            ], 422);
        }

        // 请求异常
        if ($e instanceof HttpException) {
            return json([
                'code' => $e->getStatusCode(),
                'msg' => $e->getMessage(),
                'data' => $e->getHeaders()
            ], $e->getStatusCode());
        }

        // 其他错误交给系统处理
        return parent::render($e);
    }
}
